package edu.uiowa.smallworld;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import edu.uiowa.smallworld.ViewConnectionsActivity.GetConnectionsTask;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;
import android.widget.Toast;

public class WifiDirectBroadcastReciever extends BroadcastReceiver{
	
	private WifiP2pManager mManager;
	private Channel mChannel;
	private Peer2PeerService mService;
	private List<WifiP2pDevice> mPeers = new ArrayList<WifiP2pDevice>();
	private boolean bFirst = true;
	
	public List<WifiP2pDevice> peersToCheck = new ArrayList<WifiP2pDevice>();
	
	public WifiDirectBroadcastReciever(WifiP2pManager manager, Channel channel, Peer2PeerService service)
	{
		super();
		mManager = manager;
		mChannel = channel;
		mService = service;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		
		if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action))
		{
			// check to see if Wi-Fi is enabled and notify appropriate activity
			int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
			if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED)
			{
				// wifi p2p is enabled
			}
			else
			{
				// wifi p2p is not enabled
			}
		}
		else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action))
		{
			// get a copy of the list before the update
			Collection<WifiP2pDevice> oldPeers = mPeers;
			
			// perform the update
			if(mManager != null)
			{
				mManager.requestPeers(mChannel, peerListListener);
			}		
		}
		else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action))
		{
			// respond to new connection or disconnections
			
		}
		else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action))
		{
			// respond to this device's wifi state changing
		}
		
	}
	
	private PeerListListener peerListListener = new PeerListListener() {
		
		@Override
		public void onPeersAvailable(WifiP2pDeviceList peerList) {
			// "out with the old, in with the new."
			Log.d("Service", "requesting peers");
			Log.d("Service", "device list size: " + peerList.getDeviceList().size());
			
			
			List<WifiP2pDevice> oldPeers = new ArrayList<WifiP2pDevice>(mPeers);

			mPeers.clear();
			mPeers.addAll(peerList.getDeviceList());	

			List<WifiP2pDevice> newPeers = new ArrayList<WifiP2pDevice>(mPeers);
			
			List<WifiP2pDevice> outgoing = new ArrayList<WifiP2pDevice>();
			List<WifiP2pDevice> incoming = new ArrayList<WifiP2pDevice>();
			
			for(int i = 0; i < oldPeers.size(); i++) {
				WifiP2pDevice device = oldPeers.get(i);
				if(!newPeers.contains(device)) {
					Log.d("outgoing", device.deviceName);
					outgoing.add(device);
				}
			}
			
			for(int i = 0; i < newPeers.size(); i++) {
				WifiP2pDevice device = newPeers.get(i);
				if(!oldPeers.contains(device)) {
					Log.d("incoming", device.deviceName);
					incoming.add(device);
				}
			}
			
			for(int i = 0; i < peersToCheck.size(); i++) {
				Log.d("peers2check before", peersToCheck.get(i).deviceName);
			}
			
			peersToCheck.removeAll(outgoing);
			peersToCheck.addAll(incoming);
			
			for(int i = 0; i < peersToCheck.size(); i++) {
				Log.d("peers2check after", peersToCheck.get(i).deviceName);
			}
		}
	};

}
